<?php
/**
 * Created by PhpStorm.
 * Admininfo: Administrator
 * Date: 2017/12/5
 * Time: 11:34
 */
namespace app\admin\controller;
use app\common\controller\Baseadmin;
use think\Config;
use think\Request;
use think\Session;
use  app\admin\model\Admininfo;
class Index extends Baseadmin
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }
    public  function  index(){
        if($this->isLogin()){
            $this->redirect('Controller/index');
        }else{
            return $this->fetch('login');
        }
    }
    //后台登录接口
    public  function login(){
            if(	$this->request->isPost()){
                $requestdata=$this->request->param();
                if(!$requestdata['captcha']){return json(['msg'=>'验证码不能为空','code'=>0]);};
                if(!$this->check_verify($requestdata['captcha'])){return json(['msg'=>'验证码不正确请重新输入','code'=>0]);}
                if(!$requestdata['username'] or !$requestdata['password']){return json(['msg'=>'账号和密码不能为空','code'=>0]);}
                $Usermodel=new  Admininfo();
                $salt=Config::get('salt');
                $password = md5($salt.$requestdata['password']);
                $res=$Usermodel->where(['name'=>$requestdata['username'],'pass'=>$password])->find();
                if($res && $res['status']==1 ){
                    Session::set('userid',$res['id']);
                    Session::set('groupid',$res['user_group_id']);
                    Session::set('name',$res['name']);
                    Session::set('userinfo',$res);
                    $this->adduserlog();
                    $this->success('登录成功','Controller/index');
                }elseif($res && $res['status']!=1){
                    return json(['msg'=>'次账号被拉黑，请联系管理员','code'=>0]);
                }else{
                    return json(['msg'=>'账号或密码错误，请重新登录！','code'=>0]);
                }
            }else{
                return $this->fetch();
            }
        }
}